﻿using System;

using System.Collections;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.ContentRender;

namespace RAP.Framework.Libary.DomTemplateBuilder.Configuration
{
	/// <summary>
	/// 配置文档对象（DomTemplate）
	/// </summary>
	public class DomTemplate : Hashtable, IDisposable
    {
        public DomTemplate ( )
        {
            this.CustomerHandlers = new Hashtable ( );
            this.CustomerElementMappings = new Hashtable ( );
        }

        /// <summary>
        /// 
        /// </summary>
        public DomTemplateManager TemplateManager { get; set; }
		
        /// <summary>
        /// 名称空间
        /// </summary>
        public string NameSpace
        {
            get
            {
                return this.ToString ( "NameSpace" );
            }
            set
            {
				if ( value.IsNullOrEmpty ( ) )
                {
                    throw new NullReferenceException ( "Error!DomTemplate's attribute 'NameSpace' is not allowed null or empty string." );
                }
                this[ "NameSpace" ] = value;
            }
        }

		private IContentRender _contentRender = null;
        /// <summary>
        /// 解析文本内容的内容处理器
        /// </summary>
		public IContentRender ContentRender
        {
            get
            {
                if ( _contentRender == null )
                {
					lock ( this )
					{
						if ( _contentRender == null )
						{
							string patternRender = this.ToString ( Consts.CONTENT_RENDER_FIELD );
							if ( patternRender.IsNullOrEmpty ( ) )
							{
								_contentRender = DomTemplateManager.DefaultContentRender;
							}
							else
							{
								_contentRender = DomTemplateManager.GetContentRender ( patternRender );
							}
						}
					}
                }
                return _contentRender;
            }
			set
			{
				_contentRender = value;
			}
        }

        /// <summary>
        /// 自定义的Handler
        /// </summary>
        public IDictionary CustomerHandlers { get; set; }

        /// <summary>
        /// 自定义元素类型映射
        /// </summary>
        public IDictionary CustomerElementMappings { get; set; }

        /// <summary>
        /// 根据节点名称返回节点的类型
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public Type GetElementType ( string nodeName )
        {
            Type type = null;

            type = this.CustomerElementMappings[ nodeName ] as Type;
            if ( type == null )
            {
                return DomTemplateManager.GetElementType ( nodeName );
            }
            return type;
        }

        /// <summary>
        /// 根据节点的Handler类型
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public Type GetHandlerType ( string nodeName )
        {
            Type type = null;
			if ( nodeName.IsNullOrEmpty ( ) )
			{
				return null;
			}
            type = this.CustomerHandlers[ nodeName ] as Type;
            if ( type == null )
            {
                return DomTemplateManager.GetHandlerType ( nodeName );
            }
            return type;
        }
        
        public void Dispose ( )
        {
            _contentRender = null;
            this.CustomerHandlers.Clear ( );
            this.CustomerHandlers = null;
            this.CustomerElementMappings.Clear ( );
            this.CustomerElementMappings = null;
        }
    }
}
